﻿using SmartSpace.Silverlight;
using System.Windows.Media.Imaging;
using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Browser;
using SmartSpace.Silverlight.Shell.DefaultTheme.Utils;

namespace SmartSpace.Silverlight.Shell.DefaultTheme
{
    public partial class MainPanel : SlView, IMainPanel, ISlViewContainerObserver
    {
        #region Variables

        private static double FashtionWorkAreaHeight = 500;

        private static double TotalRedundantHeight = 0;

        private NavigatePanel m_navigatePanel;
        private WorkAreaPanel m_workAreaPanel;

        private MainPanelUtil _util;
        #endregion

        public MainPanel()
        {
            InitializeComponent();

            _util = new MainPanelUtil();
            _util.Register(typeof(NavigatePanel), _ctlBtnShowNavigation);
            _util.Register(typeof(WorkAreaPanel), _ctlBtnShowWorkArea);

            m_navigatePanel = new NavigatePanel();
            m_workAreaPanel = new WorkAreaPanel();
            _ctlViewContainer.SwitchEffect = new HorizontalSwitchEffect();
            _ctlViewContainer.SwitchView(m_navigatePanel);

            this.m_workAreaPanel.GetSlViewContainer().Attach(this);
            this._ctlViewContainer.Attach(_util);

            //System.Windows.Media.ImageBrush brush = new System.Windows.Media.ImageBrush();
            //brush.Stretch = System.Windows.Media.Stretch.UniformToFill;
            //brush.ImageSource = new BitmapImage(new Uri("./Resources/background.png", UriKind.RelativeOrAbsolute));
            //_ctlBackground.Background = brush;
        }

        #region IMainPanel Members

        public INavigatePanel GetNavigatePanel()
        {
            return m_navigatePanel;
        }

        public IWorkAreaPanel GetWorkAreaPanel()
        {
            return m_workAreaPanel;
        }

        public IHeaderPanel GetHeaderPanel()
        {
            return m_headerPanel;
        }

        public IFooterPanel GetFooterPanel()
        {
            return m_footerPanel;
        }

        public SlView GetContentView()
        {
            return this;
        }

        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlViewContainer.Views.Count > 0)
            {
                if (_ctlViewContainer.CurrentView != m_navigatePanel)
                {
                    _ctlViewContainer.SwitchView(m_navigatePanel, SwitchDirection.Backward);
                }
            }
            else
            {
                _ctlViewContainer.SwitchView(m_navigatePanel, SwitchDirection.Backward);
            }
        }

        private void ButtonWorkArea_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlViewContainer.Views.Count > 0)
            {
                if (_ctlViewContainer.CurrentView != m_workAreaPanel)
                {
                    _ctlViewContainer.SwitchView(m_workAreaPanel);
                }
            }
            else
            {
                _ctlViewContainer.SwitchView(m_workAreaPanel);
            }
        }

        public void Update(SlViewContainer container)
        {
            if (container.Views.Count > 0)
            {
                _ctlBtnShowWorkArea.Visibility = System.Windows.Visibility.Visible;
                _ctlViewContainer.SwitchView(m_workAreaPanel);
            }
            else
            {
                _ctlBtnShowWorkArea.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}
